#include "04_pimpl.h"
#include <iostream>
using std::cout;
using std::endl;

class Line::LineImpl{   // 此处需要完整定义，而非单纯的写LineImpl
public:
    // 构造函数
    LineImpl(int x1, int y1, int x2, int y2)
        :pt1(Point(x1, y1))     // 此处需要显示构造Point对象
        ,pt2(Point(x2, y2))
    {}

    void printPoints(){
        pt1.printPoint();
        cout << "----->";
        pt2.printPoint();
        cout << endl;
    }
private:
    // 嵌套类
    class Point{
    public:
        Point(int x, int y)
            :_x(x)
            ,_y(y)
        {}
      
        void printPoint(){
            cout << "(" << _x << "," << _y << ")"; 
        }
    private:
        int _x;
        int _y;
    };

    // 私有成员
    Point pt1;
    Point pt2;
};

Line::Line(int x1, int y1, int x2, int y2)
    :_impl(new LineImpl(x1, y1, x2, y2)){
    }

Line::~Line(){
    if(_impl){
        delete _impl; 
        _impl = nullptr; 
    }
}

void Line::printLine(){
    //Line::LineImpl::printPoints(); // 报错
    _impl->printPoints();   // 委托调用
}

